//
//  NetworkUtils.m
//

#import "NetworkUtils.h"
#import "JSONUtils.h"
#import "AppDelegate.h"

@interface NetworkUtils ()

@end

@implementation NetworkUtils

+ (void)handleParam:(NSMutableDictionary *)param {
    //拼接sign字符串
//    param[@"platform"] = @"app";
	
//    NSArray *values = [param.allValues sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
	
	
//    NSMutableString *signString = [[NSMutableString alloc]initWithString:@""];
//    for (int i=0; i<values.count; i++) {
//        NSString *value = values[i];
//        [signString appendString:[NSString stringWithFormat:@"%@",value]];
//    }
//    if ([GlobalLogic sharedInstance].user) {
//        [signString appendString:[NSString stringWithFormat:@"token=%@&",[GlobalLogic sharedInstance].user[@"token"]]];
//    }
//    [signString appendString:[NSString stringWithFormat:@"appkey=%@",AppKey]];
	
    //加密sign字符串
//    NSString *signResultString = signString.sha256.uppercaseString.md5.uppercaseString;
//	[signString appendString:@"JiaZhangXueYuan"];
////    NSLog(@"signString---%@",signString);
//	NSString *signStr = [signString md5];
	
	
//    NSLog(@"md5-----%@",signStr);
//    //添加最后几个参数
//    [param setObject:signStr forKey:@"sign"];
//    if ([GlobalLogic sharedInstance].user) {
//        [param setObject:[GlobalLogic sharedInstance].user[@"token"] forKey:@"token"];
//    }
}


+ (NSDictionary *)handleParameter:(NSArray *)parameter {
    NSMutableDictionary *Mdict = [[NSMutableDictionary alloc] init];
    NSMutableString *Mstring = [[NSMutableString alloc] initWithString:@""];
    for (int i = 0; i < parameter.count; i++) {
        if (i%2 == 1) {
            NSString *key = [NSString stringWithFormat:@"%@",parameter[i-1]];
            NSString *value = [NSString stringWithFormat:@"%@",parameter[i]];
            [Mdict setObject:value forKey:key];
            if ([self isPingLun:key]||[self IsChinese:value]) {
                continue;
            } else {
                [Mstring appendString:value];
            }
        }
    }
    [Mstring appendString:@"JiaJu"];
    NSString *string = [Mstring md5];
    [Mdict setValue:string forKey:@"Sign"];
    return Mdict.copy;
}

+ (BOOL)isPingLun:(NSString *)key {
    if ([key isEqualToString:@"Title"] || [key isEqualToString:@"Info"] || [key isEqualToString:@"PingLunInfo"] || [key isEqualToString:@"PinglunInfo"] || [key isEqualToString:@"AnswerInfo"] || [key isEqualToString:@"NeiRong"] || [key isEqualToString:@"ZiXunInfo"] || [key isEqualToString:@"Str"] || [key isEqualToString:@"BiJiInfo"] || [key isEqualToString:@"BiaoTi"] || [key isEqualToString:@"Name"] || [key isEqualToString:@"ChildrenName"] || [key isEqualToString:@"BeiZhu"]) {
        return YES;
    }
    return NO;
}

//判断是否有中文
+ (BOOL)IsChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;
        }
        
    }
    return NO;
    
}

+ (void)requestWithMethod:(NSString *)method URL:(NSString *)URLString param:(NSArray *)param completionHandler:(void (^)(id, NSError *))handler
{
//    NSMutableDictionary *Mdict = [[NSMutableDictionary alloc] initWithDictionary:param];
	URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSDictionary *parameter;
    if (param) {
        parameter = [self handleParameter:param];
    }
    NSURL *URL = [NSURL URLWithString:URLString];
    
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",nil];
    //超时时间
    manager.requestSerializer.timeoutInterval = 15.0;
    
    if ([method isEqualToString:@"GET"]) {
        NSMutableDictionary *parameterM = [[NSMutableDictionary alloc] initWithDictionary:parameter];
        [parameterM removeObjectForKey:@"Sign"];
        [manager GET:URL.absoluteString parameters:parameterM.copy progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//            id object = [JSONUtils objectFromJSONString:encodeString];
            if (handler) {
                handler(encodeString,nil);//object
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"Error: %@", error);
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            if (handler) {
                handler(nil,error);
            }
        }];
    }
    
    else if ([method isEqualToString:@"POST"]) {
        [manager POST:URL.absoluteString parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSString *encodeStringChecked = [JSONUtils checkJsonString:encodeString];
            id object = [JSONUtils objectFromJSONString:encodeStringChecked];
            NSLog(@"%@",object);
            if (handler) {
                handler(object,nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSLog(@"Error: %@", error);
            if (handler) {
                handler(nil,error);
            }
        }];
    }
    
    else if ([method isEqualToString:@"DELETE"]) {
        NSLog(@"param: %@",[JSONUtils JSONStringFromObject:param]);
        [manager DELETE:URL.absoluteString parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            id object = [JSONUtils objectFromJSONString:encodeString];
            NSLog(@"%@",object);
            if (handler) {
                handler(object,nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSLog(@"Error: %@", error);
            if (handler) {
                handler(nil,error);
            }
        }];
    }
}

+ (void)requestNOHandleWithMethod:(NSString *)method URL:(NSString *)URLString param:(NSDictionary *)parameter completionHandler:(void (^)(id, NSError *))handler
{
    //    NSMutableDictionary *Mdict = [[NSMutableDictionary alloc] initWithDictionary:param];
    if (![[AFNetworkReachabilityManager sharedManager] isReachable] && ![URLString isEqualToString:cxxxForApp]) {
        [MBProgressHUD showTipMessageInWindow:@"网络异常"];
        return;
    }
    URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    NSDictionary *parameter;
//    if (param) {
//        parameter = [self handleParameter:param];
//    }
    NSURL *URL = [NSURL URLWithString:URLString];
    
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",nil];
    //超时时间
    manager.requestSerializer.timeoutInterval = 15.0;
    
    __weak __typeof(&*self)weakSelf = self;
    if ([method isEqualToString:@"GET"]) {
        [manager GET:URL.absoluteString parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            id object = [JSONUtils objectFromJSONString:encodeString];
            BOOL nullPointer = [weakSelf checkForNullPointer:object];
            if (nullPointer) {
                NSLog(@"nullPointerURL---%@",URLString);
                [[NSNotificationCenter defaultCenter] postNotificationName:UINotificationWillGoLoginMessageName object:nil];
                [[UIViewController currentViewController].navigationController popToRootViewControllerAnimated:false];
                if (handler) {
                    NSDictionary *object = @{@"code":@"5821",@"msg":@"登录查看"};
                    handler(object,nil);
                }
            } else {
                if (handler) {
                    handler(object,nil);
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"Error: %@", error);
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            if (handler) {
                handler(nil,error);
            }
        }];
    }
    
    else if ([method isEqualToString:@"POST"]) {
        [manager POST:URL.absoluteString parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            id object = [JSONUtils objectFromJSONString:encodeString];
            NSLog(@"%@",object);
            BOOL nullPointer = [weakSelf checkForNullPointer:object];
            if (nullPointer) {
                NSLog(@"nullPointerURL---%@",URLString);
                [[NSNotificationCenter defaultCenter] postNotificationName:UINotificationWillGoLoginMessageName object:nil];
                [[UIViewController currentViewController].navigationController popToRootViewControllerAnimated:false];
                if (handler) {
                    NSDictionary *object = @{@"code":@"5821",@"msg":@"登录查看"};
                    handler(object,nil);
                }
            } else {
                if (handler) {
                    handler(object,nil);
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSLog(@"Error: %@", error);
            if (handler) {
                handler(nil,error);
            }
        }];
    }
    
    else if ([method isEqualToString:@"DELETE"]) {
//        NSLog(@"param: %@",[JSONUtils JSONStringFromObject:param]);
        [manager DELETE:URL.absoluteString parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            id object = [JSONUtils objectFromJSONString:encodeString];
            NSLog(@"%@",object);
            if (handler) {
                handler(object,nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
            NSLog(@"Error: %@", error);
            if (handler) {
                handler(nil,error);
            }
        }];
    }
}


+ (BOOL)checkForNullPointer:(id)object {
    /*
         {
             code = 500;
             data =     {
                 errMsg = "NullPointerException:null";
             };
             msg = "请求失败";
         }
     */
    NSNumber *status = [object objectForKey:@"code"];
    if ([status integerValue] == 500) {
        NSDictionary *dataDict = [object objectForKey:@"data"];
        if (![dataDict isKindOfClass:[NSNull class]] && dataDict) {
            NSString *errMsg = [ValidateUtils validateNull:dataDict[@"errMsg"]];
            if ([errMsg rangeOfString:@"null"].length > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}


+ (void)uploadImageWithURL:(NSString*)URLString
                    param:(NSArray*)param
                     data:(NSArray*)data
                     name:(NSArray*)name
                 filename:(NSArray*)filename
        completionHandler:(void (^)(id, NSError *))handler
{
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSLog(@"net: %@",URLString);
    NSDictionary *parameter;
    if (param) {
        parameter = [self handleParameter:param];
    }
    NSMutableURLRequest *requestUpLoad = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLString parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        for (int i=0; i<[data count]; i++) {
            [formData appendPartWithFileData:data[i] name:name[i] fileName:filename[i] mimeType:@"image/jpeg"];//@"application/octet-stream"
        }
    } error:nil];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSURLSessionUploadTask *uploadTask = [manager  uploadTaskWithStreamedRequest:requestUpLoad
                  progress:^(NSProgress * _Nonnull uploadProgress) {
                      
                  }
                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                      [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
                      if (error) {
                          NSLog(@"error: %@",error);
                          if (handler) {
                              handler(nil,error);
                          }

                      } else {
                          NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
                          NSDictionary *object = [JSONUtils objectFromJSONString:encodeString];
                          NSLog(@"data: %@",object);
                          if (handler) {
                              handler(object,nil);
                          }
                      }
                  }];
    [uploadTask resume];
}

+ (void)uploadImageNoHandleWithURL:(NSString*)URLString
                     param:(NSMutableDictionary*)param
                      data:(NSArray*)data
                      name:(NSArray*)name
                  filename:(NSArray*)filename
         completionHandler:(void (^)(id, NSError *))handler
{
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSLog(@"net: %@",URLString);
//    NSDictionary *parameter;
//    if (param) {
//        parameter = [self handleParameter:param];
//    }
    NSMutableURLRequest *requestUpLoad = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        for (int i=0; i<[data count]; i++) {
            [formData appendPartWithFileData:data[i] name:name[i] fileName:filename[i] mimeType:@"image/jpeg"];//@"application/octet-stream"
        }
    } error:nil];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSURLSessionUploadTask *uploadTask = [manager  uploadTaskWithStreamedRequest:requestUpLoad
                                                                        progress:^(NSProgress * _Nonnull uploadProgress) {
                                                                            
                                                                        }
                                                               completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                                                                   [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
                                                                   if (error) {
                                                                       NSLog(@"error: %@",error);
                                                                       if (handler) {
                                                                           handler(nil,error);
                                                                       }
                                                                       
                                                                   } else {
                                                                       NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
                                                                       NSDictionary *object = [JSONUtils objectFromJSONString:encodeString];
                                                                       NSLog(@"data: %@",object);
                                                                       if (handler) {
                                                                           handler(object,nil);
                                                                       }
                                                                   }
                                                               }];
    [uploadTask resume];
}

+ (void)uploadFileNoHandleWithURL:(NSString*)URLString
                             param:(NSMutableDictionary*)param
                              data:(NSArray*)data
                              name:(NSArray*)name
                          filename:(NSArray*)filename
                 completionHandler:(void (^)(id, NSError *))handler
{
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSLog(@"net: %@",URLString);
    //    NSDictionary *parameter;
    //    if (param) {
    //        parameter = [self handleParameter:param];
    //    }
    NSMutableURLRequest *requestUpLoad = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        for (int i=0; i<[data count]; i++) {
            NSString *filenameString = filename[i];
            NSString *mimeTypeString = [filenameString componentsSeparatedByString:@"."].lastObject;
            NSDictionary *mimeTypeDict =  @{@".3gp":@"video/3gpp",
                                    @".apk":@"application/vnd.android.package-archive",
                                    @".asf":@"video/x-ms-asf",
                                    @".avi":@"video/x-msvideo",
                                    @".bin":@"application/octet-stream",
                                    @".bmp":@"image/bmp",
                                    @".c":@"text/plain",
                                    @".class":@"application/octet-stream",
                                    @".conf":@"text/plain",
                                    @".cpp":@"text/plain",
                                    @".doc":@"application/msword",
                                    @".docx":@"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                                    @".xls":@"application/vnd.ms-excel",
                                    @".xlsx":@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                    @".exe":@"application/octet-stream",
                                    @".gif":@"image/gif",
                                    @".gtar":@"application/x-gtar",
                                    @".gz":@"application/x-gzip",
                                    @".h":@"text/plain",
                                    @".htm":@"text/html",
                                    @".html":@"text/html",
                                    @".jar":@"application/java-archive",
                                    @".java":@"text/plain",
                                    @".jpeg":@"image/jpeg",
                                    @".jpg":@"image/jpeg",
                                    @".js":@"application/x-javascript",
                                    @".log":@"text/plain",
                                    @".m3u":@"audio/x-mpegurl",
                                    @".m4a":@"audio/mp4a-latm",
                                    @".m4b":@"audio/mp4a-latm",
                                    @".m4p":@"audio/mp4a-latm",
                                    @".m4u":@"video/vnd.mpegurl",
                                    @".m4v":@"video/x-m4v",
                                    @".mov":@"video/quicktime",
                                    @".mp2":@"audio/x-mpeg",
                                    @".mp3":@"audio/x-mpeg",
                                    @".mp4":@"video/mp4",
                                    @".mpc":@"application/vnd.mpohun.certificate",
                                    @".mpe":@"video/mpeg",
                                    @".mpeg":@"video/mpeg",
                                    @".mpg":@"video/mpeg",
                                    @".mpg4":@"video/mp4",
                                    @".mpga":@"audio/mpeg",
                                    @".msg":@"application/vnd.ms-outlook",
                                    @".ogg":@"audio/ogg",
                                    @".pdf":@"application/pdf",
                                    @".png":@"image/png",
                                    @".pps":@"application/vnd.ms-powerpoint",
                                    @".ppt":@"application/vnd.ms-powerpoint",
                                    @".pptx":@"application/vnd.openxmlformats-officedocument.presentationml.presentation",
                                    @".prop":@"text/plain",
                                    @".rc":@"text/plain",
                                    @".rmvb":@"audio/x-pn-realaudio",
                                    @".rtf":@"application/rtf",
                                    @".sh":@"text/plain",
                                    @".tar":@"application/x-tar",
                                    @".tgz":@"application/x-compressed",
                                    @".txt":@"text/plain",
                                    @".wav":@"audio/x-wav",
                                    @".wma":@"audio/x-ms-wma",
                                    @".wmv":@"audio/x-ms-wmv",
                                    @".wps":@"application/vnd.ms-works",
                                    @".xml":@"text/plain",
                                    @".z":@"application/x-compress",
                                    @".zip":@"application/x-zip-compressed"};
            NSString *mimeType = mimeTypeDict[[NSString stringWithFormat:@".%@",mimeTypeString]];
            [formData appendPartWithFileData:data[i] name:name[i] fileName:filenameString mimeType:mimeType];
        }
    } error:nil];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSURLSessionUploadTask *uploadTask = [manager  uploadTaskWithStreamedRequest:requestUpLoad
                                                                        progress:^(NSProgress * _Nonnull uploadProgress) {
                                                                            
                                                                        }
                                                               completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                                                                   [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
                                                                   if (error) {
                                                                       NSLog(@"error: %@",error);
                                                                       if (handler) {
                                                                           handler(nil,error);
                                                                       }
                                                                       
                                                                   } else {
                                                                       NSString *encodeString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
                                                                       NSDictionary *object = [JSONUtils objectFromJSONString:encodeString];
                                                                       NSLog(@"data: %@",object);
                                                                       if (handler) {
                                                                           handler(object,nil);
                                                                       }
                                                                   }
                                                               }];
    [uploadTask resume];
}



+ (void)downloadFileWithURL:(NSString *)URLString
                   filePath:(NSString *)filePath
                   progress:(void (^)(NSProgress *))downloadProgressBlock
          completionHandler:(void (^)(id, NSError *))handler{
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    NSLog(@"net: %@",URLString);
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURL *URL = [NSURL URLWithString:URLString];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        downloadProgressBlock(downloadProgress);
    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSLog(@"默认下载地址:%@  %@",targetPath,filePath);
        return [NSURL fileURLWithPath:filePath];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
        if (error) {
            if (handler) {
                handler(nil,error);
            }
        }
        else{
            if (handler) {
                handler(filePath,nil);
            }
        }
    }];
    [downloadTask resume];
}


+ (void)reachabilityStatusChange{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
    }];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}


@end
